package com.shopping.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import com.shopping.dao.ClientDao;
import com.shopping.dao.impl.ClientDaoImpl;
import com.shopping.entity.Book;
import com.shopping.entity.Favorite;
import com.shopping.entity.PageBean;
import com.shopping.entity.User;
import com.shopping.service.ClientService;

public class ClientServiceImpl implements ClientService {

	private ClientDao dao = new ClientDaoImpl();

	@Override
	public User login(String username, String password) {
		return dao.login(username, password);
	}

	@Override
	public boolean register(String username, String password, String name, String sex, String tel, String address) {
		User user = new User(UUID.randomUUID().toString(), username, password, name, sex, tel, address);
		return dao.register(user);
	}

	@Override
	public PageBean<Book> findBooktype(String type,int currentPage,int currentCount){
		PageBean<Book> pageBean = new PageBean<Book>();
		//当前页数
		pageBean.setCurrentPage(currentPage);
		//当前页显示条数
		pageBean.setCurrentCount(currentCount);
		//获取总条数
		int totalCount = 0;

		totalCount = dao.getCount(type);

		pageBean.setTotalCount(totalCount);
		//总页数
		int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
		pageBean.setTotalPage(totalPage);
		int start = (currentPage-1)*currentCount;
		//当页商品，并且默认是查找第一页的商品列表，根据分页机制，需要算出起始条数和一页中的数量
		List<Book> list = null;
		list = dao.findBooktype(type,start,currentCount);
		pageBean.setList(list);
		return pageBean;

		//return dao.findBooktype(type,start,count);
	}

    @Override
    public PageBean<Book> search(String search,int currentPage,int currentCount) {
        PageBean<Book> pageBean = new PageBean<Book>();
        //当前页数
        pageBean.setCurrentPage(currentPage);
        //当前页显示条数
        pageBean.setCurrentCount(currentCount);
        //获取总条数
        int totalCount = 0;
        totalCount = dao.getSearchCount(search);
        pageBean.setTotalCount(totalCount);
        //总页数
        int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
        pageBean.setTotalPage(totalPage);
        int start = (currentPage-1)*currentCount;
        //当页商品，并且默认是查找第一页的商品列表，根据分页机制，需要算出起始条数和一页中的数量
        List<Book> list = null;
        list = dao.search(search,start,currentCount);
        pageBean.setList(list);
        return pageBean;
    }

	@Override
	public void personInformation(String username, String name, String sex, String tel, String address) {
		User user = new User(null, username, null, name, sex, tel, address);
		dao.personInformation(user);
	}

	@Override
	public void personPassword(String password, String username) {
		User user = new User(null, username, password, null, null, null, null);
		dao.personPassword(user);
	}

    @Override
    public List<Book> xmlbc() {
        return dao.xmlbc();
    }

    @Override
    public List<Book> jsbc() {
        return dao.jsbc();
    }

    @Override
    public List<Book> dbDesign() {
        return dao.dbDesign();
    }

    @Override
    public List<Book> jsjwl() {
        return dao.jsjwl();
    }

    @Override
    public List<Book> kxjs() {
        return dao.kxjs();
    }

    @Override
    public List<Book> javabc() {
        return dao.javabc();
    }

	@Override
	public Book findBookById(String book_id) {
		return dao.findBookById(book_id);
	}

	@Override
	public void addfavorite(String user_id, String book_id) {
		dao.addfavorite(UUID.randomUUID().toString(), user_id, book_id);
	}

	@Override
	public List<Favorite> findFavoriteByUserId(User user) {
		return dao.findFavoriteByUserId(user);
	}

	@Override
	public boolean findFavorite(String user_id, String book_id) {
		return dao.findFavorite(user_id, book_id);
	}

	@Override
	public void delFavorite(String book_id) {
		dao.delFavorite(book_id);

	}

}
